if(!basecampWidget){
	var basecampWidget = {};
}

basecampWidget.extractIDFromURL = function( type, url ){
	var commonRegExp = /\/(\d+)\.xml$/i ;//can be used for most types
	
	switch(type.toLowerCase()){
		case 'category':
			var returnID = url.match( commonRegExp )[1];
			return returnID;
			break;
		case 'message':
			var returnID = url.match( commonRegExp )[1];
			return returnID;
			break;
		default:
			break;
	}
	
	return '';
}


// *** START: BASECAMP API RELATED FUNCTIONS *** //
basecampWidget.getProjects = function( onSuccess, onFail ){
	// Get Projects
	new Ajax.Request('/proxy.js', {
		method: 'get',
		parameters: {	user: this.username,
						pass: this.password,
						domain: this.domain,
						use_ssl: 'true',
						resource: '/projects.xml',
						media_type: 'application/xml',
						cache_gets: 'false'
					},
		onSuccess: onSuccess,
		onFailure: onFail
	});
}

basecampWidget.getCategories = function( projectID, onSuccess, onFail ){
	// Get Project Details
	new Ajax.Request('/proxy.js', {
		method: 'get',
		parameters: {	user: this.username,
						pass: this.password,
						domain: this.domain,
						use_ssl: 'true',
						resource: '/projects/'+projectID+'/categories.xml?type=post',
						media_type: 'application/xml',
						cache_gets: 'false'
					},
		onSuccess: onSuccess,
		onFailure: onFail
	});
}

basecampWidget.createCategory = function( categoryName, categoryType, onSuccess, onFail ){
	parameters = "ticket_id="+ticket.id;
	parameters += "&pluck_param=category";
	parameters += "&category[type]="+categoryType;
	parameters += "&category[name]="+categoryName;
	
	parameters += "&user=" + basecampWidget.username;
	parameters += "&pass=" + basecampWidget.password;
	parameters += "&domain=" + basecampWidget.domain;
	parameters += "&resource=" + '/projects/'+project.basecamp.id+'/categories.xml';
	parameters += "&use_ssl=" + 'true';
	parameters += "&media_type=" + 'application/xml';
		
	new Ajax.Request('/proxy.js', {	asynchronous: true,
									evalScripts: true,
									method: 'post',
									parameters: parameters,
									onSuccess: onSuccess,
									onFailure: onFail
	});
}

basecampWidget.getMessage = function( messageID, onSuccess, onFail ){
	new Ajax.Request('/proxy.js', {
		method: 'get',
		parameters: {	user: basecampWidget.username,
						pass: basecampWidget.password,
						domain: basecampWidget.domain,
						use_ssl: 'true',
						resource: '/projects/' + project.basecamp.id + '/posts/' + messageID + '.xml',
						media_type: 'application/xml',
						cache_gets: 'false'
		},
		onSuccess: onSuccess,
		onFailure: onFail
	});
}

basecampWidget.createMessage = function( messageTitle, messageBody, messageCategoryID, messageIsPrivate, notifyIDs, onSuccess, onFail ){
	parameters = "ticket_id="+ticket.id;
	parameters += "&pluck_param=request";
	parameters += "&request[post][category-id]="+messageCategoryID;
	parameters += "&request[post][title]="+messageTitle;
	parameters += "&request[post][body]="+messageBody;
	parameters += "&request[post][extended-body]=";
	parameters += "&request[post][private]="+messageIsPrivate;
	for(var i=0; i<notifyIDs.length; i++){
		parameters += "&request[notify]=" + notifyIDs[i];
	}
	parameters += "&request[attachments]=";
	parameters += "&user=" + basecampWidget.username;
	parameters += "&pass=" + basecampWidget.password;
	parameters += "&domain=" + basecampWidget.domain;
	parameters += "&resource=" + '/projects/'+project.basecamp.id+'/posts.xml';
	parameters += "&use_ssl=" + 'true';
	parameters += "&media_type=" + 'application/xml';

	new Ajax.Request('/proxy.js', {	asynchronous: true,
									evalScripts: true,
									method: 'post',
									parameters: parameters,
									onSuccess: onSuccess,
									onFailure: onFail
	});
}

basecampWidget.updateMessage = function( messageID, messageTitle, onSuccess, onFail ){
	parameters = "ticket_id="+ticket.id;
	parameters += "&pluck_param=request";
	parameters += "&request[post][title]="+messageTitle;

	parameters += "&user=" + basecampWidget.username;
	parameters += "&pass=" + basecampWidget.password;
	parameters += "&domain=" + basecampWidget.domain;
	parameters += "&resource="+'/posts/'+messageID+'.xml';
	parameters += "&use_ssl=true";
	parameters += "&media_type=" + 'application/xml';

	new Ajax.Request('/proxy/123.js', {	asynchronous: true,
										evalScripts: true,
										method: 'put',
										parameters: parameters,
										onSuccess: onSuccess,
										onFailure: onFail
	});
}


// *** START: TODO LIST FUNCTIONS *** //
basecampWidget.getToDos = function( projectID, todoListID, onSuccess, onFail ){
	// Get Project Details
	new Ajax.Request('/proxy.js', {
		method: 'get',
		parameters: {	user: this.username,
						pass: this.password,
						domain: this.domain,
						use_ssl: 'true',
						resource: '/projects/'+projectID+'/todo_lists/'+todoListID+'.xml',
						media_type: 'application/xml',
						cache_gets: 'false'
					},
		onSuccess: onSuccess,
		onFailure: onFail
	});
}
// *** END: TODO LIST FUNCTIONS *** //



// *** END: BASECAMP API RELATED FUNCTIONS *** //
















// https://vkistudios.updatelog.com/projects/1803531/project/log
basecampWidget.projectID = 1803531;
// https://vkistudios.updatelog.com/projects/1803531/todo_lists/5312962
basecampWidget.todoListID = 5312962;




// ** START: PROJECT LIST RELATED METHODS **//
basecampWidget.retrieveProjectsList = function(){
	var onSuccessFunc = function(response){
		var projectsArray = response.responseJSON.categories;
		
		basecampWidget.projectIDAry = new Array();
		
		for( var i = 0; i < projectsArray.length; i++ ){
			basecampWidget.projectIDAry[i] = projectsArray[i].id
		}

		basecampWidget.render('chooseProject');
	}

	var onFailFunc = function(response){
		var errorMsg = "<p><strong>There was an failure connecting to "+basecampWidget.domain+"</strong></p>";
		basecampWidget.render('login',errorMsg);
	}

	basecampWidget.getProjects( onSuccessFunc, onFailFunc );
}
// ** END: PROJECT LIST RELATED METHODS **//




// ** START: TODO LIST RELATED METHODS **//
basecampWidget.viewToDoListDisplay = function(){
	if( basecampWidget.todoListID == null ){
		// search for the todo list
		
		// if a todo list was not found, then create one
		
		// 
	}

	var successFunc = function(response){
		erik = response;
		
		basecampWidget.todoList = response.responseJSON.todo_list;
		
		var todoListDisplayAry =	[
									"<h3>Basecamp To-Do List </h3>"+
									"<ul>"
									];

		if(basecampWidget.todoList.todo_items!=null){
			for(var i=0;i<basecampWidget.todoList.todo_items.length;i++){
				todoListDisplayAry[todoListDisplayAry.length] =	'<li>'+
																'<input id="basecampToDo'+basecampWidget.todoList.todo_items[i].id+'ID" name="basecampToDo'+basecampWidget.todoList.todo_items[i].id+'" value="'+basecampWidget.todoList.todo_items[i].id+'" type="checkbox" />'+
																'&nbsp;<label for="basecampToDo'+basecampWidget.todoList.todo_items[i].id+'ID">'+basecampWidget.todoList.todo_items[i].content+'</label>'+
																'</li>';
			}
		}
		
		todoListDisplayAry[todoListDisplayAry.length] =	"</ul>"+
														"<a title=\"Add an item\" href=\"#\" onclick=\"basecampWidget.render('createToDo')\">Add an item</a> ";
		
		basecampWidget.basecampWidegetBox.innerHTML = todoListDisplayAry.join("");
	}

	var failFunc = function(){
		
	}

	basecampWidget.getToDos( basecampWidget.projectID, basecampWidget.todoListID, successFunc, failFunc );

	return 'Loading..';
}


basecampWidget.createToDoDisplay = function(errorString){
	var displayArray = 	[
						'<h3>Create Basecamp To-Do</h3>',
						errorString,
						'<form id="basecampToDoWidgetCreateToDoForm" class="form" onsubmit="basecampWidget.createToDo();">',
						'<label for="">To-Do Item</label><br/>',
						'<textarea id="" name=""></textarea>',
						'<input type="submit" value="Add this item" id="basecampToDoWidgetSubmit" style="float:left;margin-top:10px;">',
						'</form>'
						];

	return displayArray.join("");
}
// ** END: TODO LIST RELATED METHODS **//




// ** START: LOGIN/LOGOUT RELATED METHODS **//
basecampWidget.loginCheck = function(){
	var username = document.getElementById('basecampWidgetUsername').value;
	var password = document.getElementById('basecampWidgetPassword').value;

	// possible verification procedure here
	basecampWidget.login(username, password);
}

basecampWidget.loginCheckResponse = function(){
	
}

basecampWidget.login = function( inputUsername, inputPassword ){
	this.username = inputUsername;
	this.password = inputPassword;

	Cookie.set('basecampWidgetUsername', inputUsername);
	Cookie.set('basecampWidgetPassword', inputPassword);

	this.render();
}

basecampWidget.logout = function(){
	this.username = null;
	this.password = null;

	Cookie.erase('basecampWidgetUsername');
	Cookie.erase('basecampWidgetPassword');
	
	this.render('login');
}
// ** END: LOGIN/LOGOUT RELATED METHODS **//



// ** START: DISPLAY METHODS **//
basecampWidget.loginDisplay = function(errorString){
	var displayArray = 	[
						'<h3>Basecamp Login</h3>',
						errorString,
						'<form id="basecampWidgetLoginForm" class="form" onsubmit="basecampWidget.loginCheck();">',
						'<label for="basecampWidgetUsername">Username</label><input type="text" id="basecampWidgetUsername"/>',
						'<label for="basecampWidgetPassword">Password</label><input type="password" id="basecampWidgetPassword"/',
						'<input type="submit" value="Login" id="basecampWidgetSubmit" style="float:left;margin-top:10px;">',
						'</form>'
						];

	return displayArray.join("");
}

basecampWidget.render = function(displayType, errorString){
	if(errorString!=null){
		errorString = '';
	}

	// manual display selection
	if(displayType != null){
		switch(displayType){
			case 'login':
				this.basecampWidegetBox.innerHTML = basecampWidget.loginDisplay(errorString);

				return;
				break;
			case 'chooseProject':
				this.basecampWidegetBox.innerHTML = basecampWidget.chooseProjectDisplay();

				return;
				break;
			case 'viewToDoList':
				this.basecampWidegetBox.innerHTML = basecampWidget.viewToDoListDisplay();

				return;
				break;
			case 'createToDo':
				this.basecampWidegetBox.innerHTML = basecampWidget.createToDoDisplay();

				return;
				break;
			case 'editToDo':
				this.basecampWidegetBox.innerHTML = basecampWidget.editToDoDisplay();

				return;
				break;
		}
	}
	
	// automatic display selection
	if ( this.username == null || this.password == null ){
		basecampWidget.render('login');
	}
	else if( basecampWidget.projectID != null ){
		basecampWidget.render('viewToDoList');
	}
}
// ** END: DISPLAY METHODS **//



basecampWidget.init = function(){
	basecampWidget.render();
}


basecampWidget.basecampWidegetBox = document.getElementById('basecampWidegetBox');












if(ticket.id == 81){
	basecampWidget.init();
}